package org.minuframework.dom4j;

import java.io.File;
import java.net.URL;
import java.util.Map.Entry;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.minuframework.lang.StringUtils;

public class XMLReader{
    
    /**
     * 读取XML文件,获得document对象
     * @param pathname 文件路径
     * @return Document
     */
    public static Document read(String pathname){
        if (StringUtils.isEmpty(pathname)) { return null; }
        try {
            SAXReader reader = new SAXReader();
            return reader.read(new File(pathname));
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 读取XML文件,获得document对象
     * @param file 文件路径
     * @return Document
     */
    public static Document read(File file){
        try {
            SAXReader reader = new SAXReader();
            return reader.read(file);
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 读取XML文件,获得document对象
     * @param url 文件路径
     * @return Document
     */
    public static Document read(URL url){
        try {
            SAXReader reader = new SAXReader();
            return reader.read(url);
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static void main(String[] args){
        System.out.println(System.getProperty("user.home"));
        Properties properties = System.getProperties();
        for (Entry<Object, Object> entry : properties.entrySet()) {
            System.out.println(entry);
        }
    }
    
}
